package com.jm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jm.common.util.entity.R;
import com.jm.common.util.entity.SelectCondition;
import com.jm.pojo.Employee;
import com.jm.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录逻辑
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody SelectCondition condition, HttpServletRequest request) {
        String username = condition.getUsername();
        String password = condition.getPassword();
//       根据用户名密码查询员工对象
        Employee employee = employeeService.getLogin(username, password);
        if (employee == null) {
//            登陆失败
            return R.error("找不到对应的用户或者,请检查你的身份或者用户名与密码");
        }
        if (employee.getStatus() == 0) return R.error("该您已被封禁，请联系管理员解除封禁");
//           登陆成功
        request.getSession().setAttribute("employee", employee.getId());
        return R.success(employee);
    }

    /**
     * 登出逻辑
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 条件分页查询
     */
    @GetMapping("/page")
    @Cacheable(value = "EmpCache",key = "#result.data")
    public R<IPage<Employee>> selectByPageCondition(SelectCondition condition) {
        IPage<Employee> employees = employeeService.selectByPageCondition(condition);
        return R.success(employees);
    }

    /**
     * 添加
     */
    @PostMapping()
    @CacheEvict(value = "EmpCache",allEntries = true)
    public R<String> addEmployee(@RequestBody Employee employee) {
        String username = employee.getUsername();
//        判断用户名是否已经存在
        boolean result = employeeService.checkUsernameIsExists(username);
        if (result) return R.error("用户名" + username + "已经存在,请更换一个用户名");
//        若这个用户名不存在则进行添加操作
        employee.setPassword("123456");
        boolean save = employeeService.save(employee);
        if (save) return R.success("添加成功");
        return R.error("添加错误");
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    @Cacheable(value = "EmpCache",key = "#id")
    public R<Employee> getById(@PathVariable String id) {
        Employee employee = employeeService.getById(id);
        if (employee == null) return R.error("根据id未获得该用户信息");
        return R.success(employee);
    }


    /**
     * 根据id修改
     */
    @PutMapping()
    @CacheEvict(value = "EmpCache",allEntries = true)
    public R<String> update(@RequestBody Employee employee) {
        boolean result = employeeService.updateById(employee);
        if (!result) return R.error("修改员工数据失败");
        return R.success("修改员工数据成功");
    }

}
